<?php
class giohang_db extends CI_Model{

    public function __construct()
    {
        $this->load->database();
    }
    
    public function get_giohang_info($idgiohang)
    {
        $query = $this->db->query("SELECT giohang.id AS IDGioHang , sanpham.ID AS IDSanPham , sanpham.TenSP , sanphamgiohang.SoLuong
                                   FROM sanpham,sanphamgiohang,giohang
                                   WHERE sanphamgiohang.idSP = sanpham.ID
                                   AND giohang.id = sanphamgiohang.idGioHang
                                   AND sanphamgiohang.idGioHang = $idgiohang");
        if($query)
            return $query->result();
        else
            return false;
    }
    
    public function get_all_donhang($iddonhang)
    {
        $query = $this->db->query("SELECT *
                                    FROM dondathang
                                    WHERE id = ".$iddonhang);
        return $query->result();
    }
    
    public function get_all_dathangsp($iddonhang)
    {
        $query = $this->db->query("SELECT *
                                    FROM dathangsp
                                    ");
        return $query->result();
    }
    
    public function update_giohang_num($idgiohang,$idsp,$soluong)
    {
        $query = $this->db->query("UPDATE sanphamgiohang
                                   SET SoLuong = $soluong
                                   WHERE idSP = '$idsp'
                                   AND idGioHang = '$idgiohang'");
        if($query)
            return true;
        else
            return false;
    }
    
    public function delete_giohang_allinfo($idgiohang)
    {
        $query = $this->db->query("DELETE FROM giohang
                                   WHERE id = '$idgiohang'");
        $query = $this->db->query("DELETE FROM sanphamgiohang
                                   WHERE idGioHang = '$idgiohang'");
        if($query)
            return true;
        else
            return false;
    }
    
    public function insert_donhang($idgiohang,$giohang,$nguoinhan,$email,$trangthai,$ghichu,$mau)
    {
        $query = $this->db->query("SELECT userID
                                   FROM giohang
                                   WHERE id = ".$idgiohang);
        $user = $query->row();
        $iddonhang = date('dmGis');
        $query = $this->db->query("INSERT INTO dondathang(id,userID,NguoiNhan,Email,TrangThai,GhiChu)
                                   VALUES ('".$iddonhang."','".$user->userID."','".$nguoinhan."','".$email."','".$trangthai."','".$ghichu."')");
        $this->db->query("DELETE FROM dondathang 
                          WHERE id=".$iddonhang);
        $query = $this->db->query("SELECT sanphamgiohang.idSP,loai.TenLoai,sanpham.TenSP,gia.Gia
                                   FROM sanphamgiohang,giohang,sanpham,loai,gia
                                   WHERE giohang.id = sanphamgiohang.idGioHang
                                   AND sanphamgiohang.idSP = sanpham.ID
                                   AND sanpham.id = gia.idSP
                                   AND sanpham.idLoai = loai.id
                                   AND giohang.id = ".$idgiohang);
        $data = $query->result();
        foreach($data as $item)
        {
            $query = $this->db->query("INSERT INTO dathangsp(id,idSP,idDonHang,TenLoai,Ten,Mau,Gia)
                                       VALUE ('NULL','".$item->idSP."','".$iddonhang."','".$item->TenLoai."','".$item->TenSP."','".$mau."','".$item->Gia."')");
        }
        
        $query = $this->db->query("DELETE FROM giohang
                                    WHERE id = ".$idgiohang);

        $query = $this->db->query("DELETE FROM sanphamgiohang
                                    WHERE idGioHang = ".$idgiohang);                            
    }

}
?>